#include <stdio.h>

void save_image(int width, int height, const char* imageFilename,unsigned char* pixels) 
{
  FILE *fp;
  const int maxVal=255; 
  
  printf("Saving image %s: %d x %d\n", imageFilename,width,height);
  fp = fopen(imageFilename,"wb");
  if (!fp) {
        printf("Unable to open file '%s'\n", imageFilename);
        return;
  }
  fprintf(fp, "P6\n");
  fprintf(fp, "%d %d\n", width, height);
  fprintf(fp, "%d\n", maxVal);

  for(int j = 0; j < height; j++) {
		  fwrite(&pixels[j*width*3], 3,width,fp);
  }

  fclose(fp);
}

